home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / daemons / nfs / nfs-serv.2be / nfs-serv / nfs-server-2.2beta16 / mkdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-21  |  2.9 KB  |  126 lines

  1. /*
  2.  * Written by Robert Rother, Mariah Corporation, August 1985.
  3.  *
  4.  * If you want it, it's yours.  All I ask in return is that if you
  5.  * figure out how to do this in a Bourne Shell script you send me
  6.  * a copy.
  7.  *                    sdcsvax!rmr or rmr@uscd
  8.  *
  9.  * Severely hacked over by John Gilmore to make a 4.2BSD compatible
  10.  * subroutine.    11Mar86; hoptoad!gnu
  11.  *
  12.  * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
  13.  * subroutine didn't return EEXIST.  It does now.
  14.  */
  15.  
  16. #ifdef HAVE_CONFIG_H
  17. #include <config.h>
  18. #endif
  19.  
  20. #include <sys/stat.h>
  21. #include <errno.h>
  22. #ifndef STD_HEADERS
  23. extern int errno;
  24. #endif
  25.  
  26. #include <sys/types.h>
  27. #if defined(HAVE_UNISTD_H) || defined(STD_HEADERS)
  28. #include <unistd.h>
  29. #endif
  30.  
  31. #ifdef _POSIX_VERSION
  32. #include <sys/wait.h>
  33. #else /* !_POSIX_VERSION */
  34. #define WIFSIGNALED(w) (((w) & 0xff) != 0x7f && ((w) & 0xff) != 0)
  35. #define WEXITSTATUS(w) (((w) >> 8) & 0xff)
  36. #endif
  37.  
  38. /*
  39.  * Make a directory.
  40.  */
  41. int
  42. mkdir (dpath, dmode)
  43.      char *dpath;
  44.      int dmode;
  45. {
  46.   int cpid, status;
  47.   struct stat statbuf;
  48.  
  49.   if (stat (dpath, &statbuf) == 0)
  50.     {
  51.       errno = EEXIST;        /* Stat worked, so it already exists */
  52.       return -1;
  53.     }
  54.  
  55.   /* If stat fails for a reason other than non-existence, return error */
  56.   if (errno != ENOENT)
  57.     return -1;
  58.  
  59.   switch (cpid = fork ())
  60.     {
  61.  
  62.     case -1:            /* Error in fork() */
  63.       return (-1);        /* Errno is set already */
  64.  
  65.     case 0:            /* Child process */
  66.       /*
  67.          * Cheap hack to set mode of new directory.  Since this
  68.          * child process is going away anyway, we zap its umask.
  69.          * FIXME, this won't suffice to set SUID, SGID, etc. on this
  70.          * directory.  Does anybody care?
  71.          */
  72.       status = umask (0);    /* Get current umask */
  73.       status = umask (status | (0777 & ~dmode));    /* Set for mkdir */
  74.       execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
  75.       _exit (-1);        /* Can't exec /bin/mkdir */
  76.  
  77.     default:            /* Parent process */
  78.       while (cpid != wait (&status));    /* Wait for kid to finish */
  79.     }
  80.  
  81.   if (WIFSIGNALED (status) || WEXITSTATUS (status) != 0)
  82.     {
  83.       errno = EIO;        /* We don't know why, but */
  84.       return -1;        /* /bin/mkdir failed */
  85.     }
  86.  
  87.   return 0;
  88. }
  89.  
  90. int
  91. rmdir (dpath)
  92.      char *dpath;
  93. {
  94.   int cpid, status;
  95.   struct stat statbuf;
  96.  
  97.   if (stat (dpath, &statbuf) != 0)
  98.     {
  99.       /* Stat just set errno.  We don't have to */
  100.       return -1;
  101.     }
  102.  
  103.   switch (cpid = fork ())
  104.     {
  105.  
  106.     case -1:            /* Error in fork() */
  107.       return (-1);        /* Errno is set already */
  108.  
  109.     case 0:            /* Child process */
  110.       execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
  111.       _exit (-1);        /* Can't exec /bin/mkdir */
  112.  
  113.     default:            /* Parent process */
  114.       while (cpid != wait (&status));    /* Wait for kid to finish */
  115.     }
  116.  
  117.   if (WIFSIGNALED (status) || WEXITSTATUS (status) != 0)
  118.     {
  119.       errno = EIO;        /* We don't know why, but */
  120.       return -1;        /* /bin/rmdir failed */
  121.     }
  122.  
  123.   return 0;
  124. }
  125.  
  126.